/**
 * 
 */
package com.youth.fund.hk.hostManager.manager.web.controller;


import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.youth.fund.hk.hostManager.common.Constants;
import com.youth.fund.hk.hostManager.manager.datamodel.CaseModel;
import com.youth.fund.hk.hostManager.manager.datamodel.ProductModel;
import com.youth.fund.hk.hostManager.manager.datamodel.TypeModel;
import com.youth.fund.hk.hostManager.manager.service.CaseService;
import com.youth.fund.hk.hostManager.manager.service.ProductService;
import com.youth.fund.hk.hostManager.manager.service.TypeService;
import com.youth.fund.hk.hostManager.manager.service.UploadService;

/**
 * @author Sunk_Sky
 *
 *         2014年11月26日下午3:16:37
 */
@Controller
@RequestMapping("/type")
public class TypeController
{
	@Autowired
	@Qualifier("TypeService")
	private TypeService service;
	@Autowired
	@Qualifier("CaseService")
	private CaseService case_service;
	@Autowired
	@Qualifier("ProductService")
	private ProductService product_service;
	
	@Autowired
	@Qualifier("UploadService")
	private UploadService uploadService;

	@ModelAttribute("types")
	public TypeModel.TypeModelType[] typeModelTypes()
	{
		return TypeModel.TypeModelType.values();
	}

	@RequiresPermissions("type:view")
	@RequestMapping(method = RequestMethod.GET)
	public String list(HttpServletRequest req, Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}

		TypeModel tm = new TypeModel();

		tm.setLang(lang);

		tm.setType(null);

		model.addAttribute("typeList", service.query(tm));

		return "type/list";
	}

	@RequiresPermissions("type:view")
	@RequestMapping(value = "/{language}", method = RequestMethod.GET)
	public String listLang(@PathVariable("language") String language,
			HttpServletRequest req, Model model)
	{
		String lang = language;

		req.getSession().setAttribute("local_lang", lang);

		TypeModel tm = new TypeModel();

		tm.setLang(lang);

		tm.setType(null);

		model.addAttribute("typeList", service.query(tm));

		return "type/list";
	}

	@RequiresPermissions("type:create")
	@RequestMapping(value = "/{parentId}/appendChild", method = RequestMethod.GET)
	public String showCreateForm(HttpServletRequest req,
			@PathVariable("parentId") int parentId, Model model)
	{

		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}
		setCommonData(model, lang);

		TypeModel parentModel = this.getParentModel(parentId, lang);

		TypeModel tm = new TypeModel();

		{
			tm.setLang(lang);

			tm.setParent(parentModel);

			tm.setParent_id(parentId);

			tm.setParent_code(parentModel.getCode());

			tm.setParent_ids(parentModel.makeSelfAsParentIds());

		}

		model.addAttribute("type", tm);

		model.addAttribute("op", "新增");

		return "type/edit";
	}

	@RequiresPermissions("type:create")
	@RequestMapping(value = "/{parentId}/appendChild", method = RequestMethod.POST)
	public String create(TypeModel model, RedirectAttributes redirectAttributes,
			@RequestParam("file") MultipartFile file)
	{
		String url = model.getLang() + "/" + model.getCode()+".html";

		model.setUrl(url);
		
		if (file.isEmpty())
		{
			url = this.uploadService.uploadImage(file, model.getName());

			model.setImg_path(url);
		}
		
		boolean result = service.create(model);

		if (result)
		{
			redirectAttributes.addFlashAttribute("msg", "新增成功");
		} else
		{
			redirectAttributes.addFlashAttribute("msg", "新增失败");
		}

		return "redirect:/type";
	}

	@RequiresPermissions("type:update")
	@RequestMapping(value = "{id}/update", method = RequestMethod.GET)
	public String showUpdateForm(HttpServletRequest req,
			@PathVariable("id") int id, Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}

		setCommonData(model, lang);

		TypeModel condition = new TypeModel();

		condition.setId(id);

		condition.setLang(lang);
		
		condition.setType(null);

		model.addAttribute("type", service.queryOne(condition));

		model.addAttribute("op", "修改");

		return "type/edit";
	}

	@RequiresPermissions("type:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	public String update(TypeModel model, RedirectAttributes redirectAttribute,
			@RequestParam("file") MultipartFile file)
	{
		String url = model.getLang() + "/" + model.getCode()+".html";

		model.setUrl(url);
		
		if (!file.isEmpty())
		{
			url = this.uploadService.uploadImage(file, model.getName());

			model.setImg_path(url);
		}

		boolean result = service.update(model);

		if (result)
		{
			redirectAttribute.addFlashAttribute("msg", "修改成功");
		} else
		{
			redirectAttribute.addFlashAttribute("msg", "修改失败");
		}

		return "redirect:/type";
	}

	@RequiresPermissions("type:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String delete(HttpServletRequest req, @PathVariable("id") int id,
			Model model, RedirectAttributes redirectAttribute)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}

		setCommonData(model, lang);

		boolean result = service.delete(id, lang);

		if (result)
		{
			redirectAttribute.addFlashAttribute("msg", "删除成功");
		} else
		{
			redirectAttribute.addFlashAttribute("msg", "删除失败");
		}

		return "redirect:/type";
	}

	private TypeModel getParentModel(int parentId, String lang)
	{
		TypeModel parentModel = new TypeModel();

		parentModel.setId(parentId);

		parentModel.setLang(lang);

		parentModel = this.service.queryOne(parentModel);

		return parentModel;
	}

	private void setCommonData(Model model, String lang)
	{
		TypeModel tm = new TypeModel();

		tm.setLang(lang);

		model.addAttribute("recruitmentList", this.service.query(tm));
	}

	@RequestMapping(value = "{id}/updateCase", method = RequestMethod.GET)
	public String showUpdateCaseForm(HttpServletRequest req,
			@PathVariable("id") int id, Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}

		setCommonData(model, lang);

		CaseModel condition = new CaseModel();

		condition.setType_id(id);

		condition.setLang(lang);

		CaseModel cm = this.case_service.queryOne(condition);

		if (cm.getType_id() == -1)
		{
			cm = new CaseModel();

			cm.setType_id(id);

			cm.setLang(lang);
		}
		model.addAttribute("case_model", cm);

		model.addAttribute("op", "修改");

		return "type/case_edit";
	}

	@RequestMapping(value = "/{id}/updateCase", method = RequestMethod.POST)
	public String updateCase(CaseModel model,
			RedirectAttributes redirectAttribute)
	{
		boolean result = this.case_service.update(model);

		if (result)
		{
			redirectAttribute.addFlashAttribute("msg", "修改成功");
		} else
		{
			redirectAttribute.addFlashAttribute("msg", "修改失败");
		}

		return "redirect:/type";
	}

	@RequestMapping(value = "{id}/updateProductList", method = RequestMethod.GET)
	public String showProductList(HttpServletRequest req,
			@PathVariable("id") int id, Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}

		setCommonData(model, lang);

		ProductModel condition = new ProductModel();

		condition.setType_id(id);

		condition.setLang(lang);

		model.addAttribute("productList", this.product_service.query(condition));

		model.addAttribute("type_id", id);

		return "type/product_list";
	}

	@RequestMapping(value = "/{id}/updateProduct", method = RequestMethod.POST)
	public String updateProduct(TypeModel model,
			RedirectAttributes redirectAttribute)
	{
		boolean result = service.update(model);

		if (result)
		{
			redirectAttribute.addFlashAttribute("msg", "修改成功");
		} else
		{
			redirectAttribute.addFlashAttribute("msg", "修改失败");
		}

		return "redirect:/type";
	}

	@RequestMapping(value = "{id}/updateRelated", method = RequestMethod.GET)
	public String showUpdateRelatedForm(HttpServletRequest req,
			@PathVariable("id") int id, Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}

		setCommonData(model, lang);

		TypeModel condition = new TypeModel();

		condition.setId(id);

		condition.setLang(lang);

		model.addAttribute("type", service.queryOne(condition));

		model.addAttribute("op", "修改");

		return "type/edit";
	}

	@RequestMapping(value = "/{id}/updateRelated", method = RequestMethod.POST)
	public String updateRelated(TypeModel model,
			RedirectAttributes redirectAttribute)
	{
		boolean result = service.update(model);

		if (result)
		{
			redirectAttribute.addFlashAttribute("msg", "修改成功");
		} else
		{
			redirectAttribute.addFlashAttribute("msg", "修改失败");
		}

		return "redirect:/type";
	}

}
